//
//  AppDelegate.m
//  miaopai
//
//  Created by Crow Zhang on 15/11/12.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "AppDelegate.h"
#import "HomePageViewController.h"
#import "HotVideoViewController.h"
#import "TopicViewController.h"
#import "MyselfViewController.h"
#import "RootViewController.h"
#import "APService.h"
#import <TAESDK/TaeSDK.h>
#import "UMSocial.h"
#import "SKSplashIcon.h"

@interface AppDelegate ()

@property (retain, nonatomic) SKSplashView *splashView;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    HomePageViewController *homePageView = [[HomePageViewController alloc] init];
    UINavigationController *homePageNav = [[UINavigationController alloc] initWithRootViewController:homePageView];
    homePageNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:nil selectedImage:nil];
    homePageNav.title = @"主页";
    
    HotVideoViewController *hotView = [[HotVideoViewController alloc] init];
    UINavigationController *hotNav = [[UINavigationController alloc] initWithRootViewController:hotView];
    hotNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"热榜" image:nil selectedImage:nil];
    hotNav.title = @"热榜";

    TopicViewController *topicView = [[TopicViewController alloc] init];
    UINavigationController *topicNav = [[UINavigationController alloc] initWithRootViewController:topicView];
    topicNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"话题" image:nil selectedImage:nil];
    
    MyselfViewController *myselfView = [[MyselfViewController alloc] init];
    UINavigationController *myselfNav = [[UINavigationController alloc] initWithRootViewController:myselfView];
    myselfNav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:nil selectedImage:nil];
    
    RootViewController *rootTabBar = [[RootViewController alloc] init];
    [rootTabBar setViewControllers:@[homePageNav, hotNav, topicNav, myselfNav]];
    
    self.window.rootViewController = rootTabBar;
    [homePageNav release];
    [homePageView release];
    [hotNav release];
    [hotView release];
    [topicNav release];
    [topicView release];
    [myselfNav release];
    [myselfView release];
    

#pragma mark 推送
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f) {
        [APService registerForRemoteNotificationTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    }else
    {
        [APService registerForRemoteNotificationTypes:UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil];
    }
    [APService setupWithOption:launchOptions];

    [[TaeSDK sharedInstance] asyncInit:^{
        
    } failedCallback:^(NSError *error) {
        NSLog(@"TaeSDK init failed!!!");
    }];

    
    [UMSocialData setAppKey:@"56554e9967e58e6ff800226b"];
    
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:homePageNav.view.frame];
//    //imageView.image = [UIImage imageNamed:@"iconfont-jingtou"];
//    [homePageNav.view addSubview:imageView];
    //Ping style splash
    UIView * view = [rootTabBar.view.subviews lastObject];
    view.frame = CGRectMake(0, kHeight + 48, kWidth, 48);
    
    SKSplashIcon *pingSplashIcon = [[SKSplashIcon alloc] initWithImage:[UIImage imageNamed:@"iconfont-jingtou.png"] animationType:SKIconAnimationTypePing];
    _splashView = [[SKSplashView alloc] initWithSplashIcon:pingSplashIcon backgroundColor:[UIColor yellowColor] animationType:SKSplashAnimationTypeBounce];
    _splashView.animationDuration = 5.0f;
    [homePageNav.view addSubview:_splashView];
    [_splashView startAnimation];
    
    [UIView animateWithDuration:0.8 delay:4.2 usingSpringWithDamping:0.9 initialSpringVelocity:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
        view.frame = CGRectMake(0, kHeight - 48, kWidth, 48);
    } completion:^(BOOL finished) {
        
    }];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) pingSplash
{
    //Setting the background
    
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [APService handleRemoteNotification:userInfo];
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    application.applicationIconBadgeNumber = 0;
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
